home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / Workbench / iconexample.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  11KB  |  315 lines

  1. ;/* iconexample.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 iconexample.c
  3. Blink FROM LIB:c.o,iconexample.o TO iconexample LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6. /*
  7. Copyright (c) 1992 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga, Inc. for
  10. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  11. published by Addison-Wesley (ISBN 0-201-56774-1).
  12.  
  13. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  14. information on the correct usage of the techniques and operating system
  15. functions presented in these examples.  The source and executable code
  16. of these examples may only be distributed in free electronic form, via
  17. bulletin board or as part of a fully non-commercial and freely
  18. redistributable diskette.  Both the source and executable code (including
  19. comments) must be included, without modification, in any copy.  This
  20. example may not be published in printed form or distributed with any
  21. commercial product.  However, the programming techniques and support
  22. routines set forth in these examples may be used in the development
  23. of original executable software products for Commodore Amiga computers.
  24.  
  25. All other rights reserved.
  26.  
  27. This example is provided "as-is" and is subject to change; no
  28. warranties are made.  All use is at your own risk. No liability or
  29. responsibility is assumed.
  30. */
  31. /*
  32. ** iconexample.c - Workbench icon startup, creation, and parsing example
  33. */
  34.  
  35. #include <exec/types.h>
  36. #include <libraries/dos.h>
  37. #include <workbench/workbench.h>
  38. #include <workbench/startup.h>
  39.  
  40. #include <clib/alib_protos.h>
  41. #include <clib/exec_protos.h>
  42. #include <clib/dos_protos.h>
  43. #include <clib/icon_protos.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47.  
  48. #ifdef LATTICE
  49. int CXBRK(void) { return(0); }   /* Disable SAS Lattice CTRL/C handling */
  50. int chkabort(void) { return(0); }/* really */
  51. #endif
  52.  
  53. /* our functions */
  54. void cleanexit(UBYTE *,LONG);
  55. void cleanup(void);
  56. void message(UBYTE *);
  57. BOOL makeIcon(UBYTE *, char **, char *);
  58. BOOL showToolTypes(struct WBArg *);
  59.  
  60. UBYTE *projname     = "RAM:Example_Project";
  61. UBYTE *conwinname   = "CON:10/10/620/180/iconexample";
  62.  
  63. UBYTE deftoolname[] = {"iconexample"};
  64.  
  65. USHORT IconImageData1[] =  {
  66. /* Plane 0 */
  67.     0x0000,0x0000,0x0000,0x1000,0x0000,0x0000,0x0000,0x3000,
  68.     0x0FFF,0xFFFC,0x0000,0x3000,0x0800,0x0004,0x0000,0x3000,
  69.     0x0800,0x07FF,0xFFC0,0x3000,0x08A8,0xA400,0x00A0,0x3000,
  70.     0x0800,0x0400,0x0090,0x3000,0x08AA,0xA400,0x0088,0x3000,
  71.     0x0800,0x042A,0xA0FC,0x3000,0x082A,0xA400,0x0002,0x3000,
  72.     0x0800,0x0400,0x0002,0x3000,0x0800,0xA42A,0xA0A2,0x3000,
  73.     0x0800,0x0400,0x0002,0x3000,0x0950,0xA42A,0x8AA2,0x3000,
  74.     0x0800,0x0400,0x0002,0x3000,0x082A,0xA400,0x0002,0x3000,
  75.     0x0800,0x042A,0x2AA2,0x3000,0x0FFF,0xFC00,0x0002,0x3000,
  76.     0x0000,0x0400,0x0002,0x3000,0x0000,0x07FF,0xFFFE,0x3000,
  77.     0x0000,0x0000,0x0000,0x3000,0x7FFF,0xFFFF,0xFFFF,0xF000,
  78. /* Plane 1 */
  79.     0xFFFF,0xFFFF,0xFFFF,0xE000,0xD555,0x5555,0x5555,0x4000,
  80.     0xD000,0x0001,0x5555,0x4000,0xD7FF,0xFFF9,0x5555,0x4000,
  81.     0xD7FF,0xF800,0x0015,0x4000,0xD757,0x5BFF,0xFF55,0x4000,
  82.     0xD7FF,0xFBFF,0xFF65,0x4000,0xD755,0x5BFF,0xFF75,0x4000,
  83.     0xD7FF,0xFBD5,0x5F01,0x4000,0xD7D5,0x5BFF,0xFFFD,0x4000,
  84.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD7FF,0x5BD5,0x5F5D,0x4000,
  85.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD6AF,0x5BD5,0x755D,0x4000,
  86.     0xD7FF,0xFBFF,0xFFFD,0x4000,0xD7D5,0x5BFF,0xFFFD,0x4000,
  87.     0xD7FF,0xFBD5,0xD55D,0x4000,0xD000,0x03FF,0xFFFD,0x4000,
  88.     0xD555,0x53FF,0xFFFD,0x4000,0xD555,0x5000,0x0001,0x4000,
  89.     0xD555,0x5555,0x5555,0x4000,0x8000,0x0000,0x0000,0x0000,
  90. };
  91.  
  92. struct Image iconImage1 =
  93.     {
  94.     0, 0,                /* Top Corner */
  95.     52, 22, 2,           /* Width, Height, Depth */
  96.     &IconImageData1[0],  /* Image Data */
  97.     0x003, 0x000,        /* PlanePick,PlaneOnOff */
  98.     NULL                 /* Next Image */
  99.     };
  100.  
  101. UBYTE *toolTypes[] =
  102.     {
  103.     "FILETYPE=text",
  104.     "FLAGS=BOLD|ITALICS",
  105.     NULL
  106.     };
  107.  
  108. struct DiskObject projIcon =
  109.     {
  110.     WB_DISKMAGIC,                   /* Magic Number */
  111.     WB_DISKVERSION,                 /* Version */
  112.         {                           /* Embedded Gadget Structure */
  113.         NULL,                       /* Next Gadget Pointer */
  114.         97,12,52,23,                /* Left,Top,Width,Height */
  115.         GADGIMAGE|GADGHBOX,         /* Flags */
  116.         GADGIMMEDIATE|RELVERIFY,    /* Activation Flags */
  117.         BOOLGADGET,                 /* Gadget Type */
  118.         (APTR)&iconImage1,          /* Render Image */
  119.         NULL,                       /* Select Image */
  120.         NULL,                       /* Gadget Text */
  121.         NULL,                       /* Mutual Exclude */
  122.         NULL,                       /* Special Info */
  123.         0,                          /* Gadget ID */
  124.         NULL                        /* User Data */
  125.         },
  126.     WBPROJECT,                      /* Icon Type */
  127.     deftoolname,                    /* Default Tool */
  128.     toolTypes,                      /* Tool Type Array */
  129.     NO_ICON_POSITION,               /* Current X */
  130.     NO_ICON_POSITION,               /* Current Y */
  131.     NULL,                           /* Drawer Structure */
  132.     NULL,                           /* Tool Window */
  133.     4000                            /* Stack Size */
  134.     };
  135.  
  136. /* Opens and allocations we must clean up */
  137. struct Library *IconBase = NULL;
  138. FILE *conwin = NULL;
  139. LONG olddir = -1;
  140.  
  141. BOOL FromWb;
  142.  
  143. void main(int argc, char **argv)
  144.     {
  145.     struct WBStartup *WBenchMsg;
  146.     struct WBArg *wbarg;
  147.     FILE  *file;
  148.     LONG  wLen;
  149.     SHORT i;
  150.  
  151.     FromWb = (argc==0) ? TRUE : FALSE;
  152.  
  153.     /* Open icon.library */
  154.     if(!(IconBase = OpenLibrary("icon.library",33)))
  155.          cleanexit("Can't open icon.library\n",RETURN_FAIL);
  156.  
  157.     /* If started from CLI, this example will create a small text
  158.      * file RAM:Example_Project, and create an icon for the file
  159.      * which points to this program as its default tool.
  160.      */
  161.     if(!FromWb)
  162.         {
  163.         /* Make a sample project (data) file */
  164.         wLen = -1;
  165.         if(file=fopen(projname,"w"))
  166.             {
  167.             wLen = fprintf(file,"Have a nice day\n");
  168.             fclose(file);
  169.             }
  170.         if(wLen < 0) cleanexit("Error writing data file\n",RETURN_FAIL);
  171.  
  172.         /* Now save/update icon for this data file */
  173.         if(makeIcon(projname, toolTypes, deftoolname))
  174.              {
  175.              printf("%s data file and icon saved.\n",projname);
  176.              printf("Use Workbench menu Icon Information to examine the icon.\n");
  177.              printf("Then copy this example (iconexample) to RAM:\n");
  178.              printf("and double-click the %s project icon\n",projname);
  179.              }
  180.         else cleanexit("Error writing icon\n",RETURN_FAIL);
  181.         }
  182.  
  183.     else  /* Else we are FromWb - ie. we were either
  184.            * started by a tool icon, or as in this case,
  185.            * by being the default tool of a project icon.
  186.            */
  187.         {
  188.         if(!(conwin = fopen(conwinname,"r+")))
  189.              cleanexit("Can't open output window\n",RETURN_FAIL);
  190.  
  191.         WBenchMsg = (struct WBStartup *)argv;
  192.  
  193.         /* Note wbarg++ at end of FOR statement steps through wbargs.
  194.          * First arg is our executable (tool).  Any additional args
  195.          * are projects/icons passed to us via either extend select
  196.          * or default tool method.
  197.          */
  198.         for(i=0, wbarg=WBenchMsg->sm_ArgList;
  199.             i < WBenchMsg->sm_NumArgs;
  200.             i++, wbarg++)
  201.             {
  202.             /* if there's a directory lock for this wbarg, CD there */
  203.             olddir = -1;
  204.             if((wbarg->wa_Lock)&&(*wbarg->wa_Name))
  205.                 olddir = CurrentDir(wbarg->wa_Lock);
  206.  
  207.             showToolTypes(wbarg);
  208.  
  209.             if((i>0)&&(*wbarg->wa_Name))
  210.                 fprintf(conwin,"In Main. We could open the %s file here\n",
  211.                                  wbarg->wa_Name);
  212.             if(olddir != -1)  CurrentDir(olddir); /* CD back where we were */
  213.             }
  214.         Delay(500);
  215.         }
  216.     cleanup();
  217.     exit(RETURN_OK);
  218.     }
  219.  
  220. BOOL makeIcon(UBYTE *name, char **newtooltypes, char *newdeftool)
  221.     {
  222.     struct DiskObject *dobj;
  223.     char *olddeftool;
  224.     char **oldtooltypes;
  225.     BOOL success = FALSE;
  226.  
  227.     if(dobj=GetDiskObject(name))
  228.         {
  229.         /* If file already has an icon, we will save off any fields we
  230.          * need to update, update those fields, put the object, restore
  231.          * the old field pointers and then free the object.  This will
  232.          * preserve any custom imagery the user has, and the user's
  233.          * current placement of the icon.  If your application does
  234.          * not know where the user currently keeps your application,
  235.          * you should not update his dobj->do_DefaultTool.
  236.          */
  237.          oldtooltypes = dobj->do_ToolTypes;
  238.          olddeftool = dobj->do_DefaultTool;
  239.  
  240.          dobj->do_ToolTypes = newtooltypes;
  241.          dobj->do_DefaultTool = newdeftool;
  242.  
  243.          success = PutDiskObject(name,dobj);
  244.  
  245.          /* we must restore the original pointers before freeing */
  246.          dobj->do_ToolTypes = oldtooltypes;
  247.          dobj->do_DefaultTool = olddeftool;
  248.          FreeDiskObject(dobj);
  249.          }
  250.     /* Else, put our default icon */
  251.     if(!success)  success = PutDiskObject(name,&projIcon);
  252.     return(success);
  253.     }
  254.  
  255. BOOL showToolTypes(struct WBArg *wbarg)
  256.     {
  257.     struct DiskObject *dobj;
  258.     char **toolarray;
  259.     char *s;
  260.     BOOL success = FALSE;
  261.  
  262.     fprintf(conwin,"\nWBArg Lock=0x%lx, Name=%s\n",
  263.                            wbarg->wa_Lock,wbarg->wa_Name);
  264.  
  265.     if((*wbarg->wa_Name) && (dobj=GetDiskObject(wbarg->wa_Name)))
  266.         {
  267.         fprintf(conwin,"  We have read the DiskObject (icon) for this arg\n");
  268.         toolarray = (char **)dobj->do_ToolTypes;
  269.  
  270.         if(s=(char *)FindToolType(toolarray,"FILETYPE"))
  271.             {
  272.             fprintf(conwin,"    Found tooltype FILETYPE with value %s\n",s);
  273.             }
  274.         if(s=(char *)FindToolType(toolarray,"FLAGS"))
  275.             {
  276.             fprintf(conwin,"    Found tooltype FLAGS with value %s\n",s);
  277.             if(MatchToolValue(s,"BOLD"))
  278.                 fprintf(conwin,"      BOLD flag requested\n");
  279.             if(MatchToolValue(s,"ITALICS"))
  280.                 fprintf(conwin,"      ITALICS flag requested\n");
  281.             }
  282.         /* Free the diskobject we got */
  283.         FreeDiskObject(dobj);
  284.         success = TRUE;
  285.         }
  286.     else if(!(*wbarg->wa_Name))
  287.         fprintf(conwin,"  Must be a disk or drawer icon\n");
  288.     else
  289.         fprintf(conwin,"  Can't find any DiskObject (icon) for this WBArg\n");
  290.     return(success);
  291.     }
  292.  
  293.  
  294. /* Workbench-started programs with no output window may want to display
  295.  * messages in a different manner (requester, window title, etc)
  296.  */
  297. void message(UBYTE *s)
  298.     {
  299.     if(FromWb && conwin)  fprintf(conwin,s,strlen(s));
  300.     else if (!FromWb) printf(s);
  301.     }
  302.  
  303. void cleanexit(UBYTE *s, LONG n)
  304.     {
  305.     if(*s)  message(s);
  306.     cleanup();
  307.     exit(n);
  308.     }
  309.  
  310. void cleanup()
  311.    {
  312.    if(conwin)    fclose(conwin);
  313.    if(IconBase)  CloseLibrary(IconBase);
  314.    }
  315.